﻿using System.Data.Common;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.Ollama;
using OllamaSharp.Models;

#pragma warning disable SKEXP0010

#pragma warning disable SKEXP0070
var endpoint = new Uri("http://localhost:11434/");
var modelId = "deepseek-r1:1.5b";
var builder = Kernel.CreateBuilder();
builder.AddOllamaChatCompletion(modelId, endpoint);



var kernel = builder.Build();
var chatService = kernel.GetRequiredService<IChatCompletionService>();

//通过在线ai接口查询结果
//var searchService = kernel.GetRequiredService<SearchSkill>();

Console.WriteLine("请输入您的问题：");
while (true)
{
    var query = Console.ReadLine();
    //Console.WriteLine("输入问题："+ query);
    // 构建提示词  
    var chatHistory = new ChatHistory();
    //在线ai接口查询接口及处理
    //List<SearchResult> result = await searchService.SearchAsync(query);
    //if (!result.Any())
    //{
    //    chatHistory.AddSystemMessage("抱歉，未找到相关搜索结果。我会基于已有知识继续为您服务。");
    //}
    //else
    //{
    //    chatHistory.AddSystemMessage($"已为您找到 {result.Count()} 条相关结果：");
    //    foreach (var item in result)
    //        chatHistory.AddSystemMessage($"• {item.Title}\n  {item.Snippet}");
    //}
    chatHistory.AddUserMessage(query);
    //Console.WriteLine(result);

    await foreach (var item in chatService.GetStreamingChatMessageContentsAsync(chatHistory))
    {
        Console.Write(item.Content);
    }
    //await foreach (var item in chatService.GetStreamingChatMessageContentsAsync(chatHistory))
    //{
    //    Console.Write(item.Content);
    //}
    Console.WriteLine("");
}



var generateService= kernel.GetRequiredService<GenerateResponseStream>();












//var endpoint = new Uri("http://localhost:11434/");
//var modelId = "qwen-plus";
//var builder = Kernel.CreateBuilder();
//builder.Services.AddScoped<HttpClient>();
//builder.Services.AddScoped<SearchSkill>();
//builder.AddOpenAIChatCompletion(modelId, endpoint.ToString());
//var kernel = builder.Build();
//var chatService = kernel.GetRequiredService<IChatCompletionService>();
//var chatHistory = new ChatHistory();
//var searchService = kernel.GetRequiredService<SearchSkill>();
//string query = "基于 vue-pure-admin 这个模板，做一个表格页面";
//List<SearchResult> result = await searchService.SearchAsync(query);
//if (!result.Any())
//{
//    chatHistory.AddSystemMessage("抱歉，未找到相关搜索结果。我会基于已有知识继续为您服务。");
//}
//else
//{
//    chatHistory.AddSystemMessage($"已为您找到 {result.Count()} 条相关结果：");
//    foreach (var item in result)
//        chatHistory.AddSystemMessage($"• {item.Title}\n  {item.Snippet}");
//}
//chatHistory.AddUserMessage(query);
//Console.WriteLine(result);
//await foreach (var item in chatService.GetStreamingChatMessageContentsAsync(chatHistory))
//{
//    Console.Write(item.Content);
//}